package org.springmvc.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springmvc.dao.UserDao;
import org.springmvc.model.User;
import org.springmvc.pagination.Page;
import org.springmvc.pagination.Pageable;

/**
 * 用户管理
 * 
 * @author 唐家林
 * @time 2014年9月5日
 */
@Service("userService")
public class UserService extends AbstractBaseService<User> {

	private UserDao userDao;

	@Resource(name = "userDao")
	public void setUserDao(UserDao userDao) {
		super.setBaseDao(userDao);
		this.userDao = userDao;
	}

	/**
	 * 通过用户名和密码获取用户信息
	 * @param name 账号
	 * @return 不存在返回null
	 */
	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public User getUser(String name) {
		return userDao.getUser(name);
	}

	public User createOrUpdate(User user) {
		if (user.getId() == null) user.setPassword("admin");
		return super.createOrUpdate(user, addIgnoreProperties("password"));
	};

	/**
	 * 获取所有用户信息
	 * @param page 分页参数
	 * @return
	 */
	public Page<User> findList(Pageable page) {
		return userDao.findList(page);
	}

}
